﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using usedcartrade.DataAccessLayer;

namespace usedcartrade.Models
{
    public class UserBusinessLayer
    {
        private UsedCarERPDAL usrdb = new UsedCarERPDAL();
        public List<User> GetUsers()
        {
            UsedCarERPDAL usersDal = new UsedCarERPDAL();
            return usersDal.users.ToList();
        }

        public UserDetails GetUserDetails(string UserName, string PassWord)
        {
            var user = usrdb.users.FirstOrDefault(u => u.userDetails.UserName == UserName
            && u.userDetails.PassWord == PassWord);
            if (user != null)
            {
                return user.userDetails;
            } else
            {
                UserDetails ud = new UserDetails();
                ud.userlevel = 2;
                return ud;
            }

        }

        public bool ifuserexist(User u)
        {
            var dbuser = usrdb.users.FirstOrDefault(ur => ur.userDetails.UserName == u.userDetails.UserName);
            return dbuser!= null;
        }

        public User SaveUser(User u)
        {
            UsedCarERPDAL usersDal = new UsedCarERPDAL();
            usersDal.users.Add(u);
            usersDal.SaveChanges();
            return u;
        }

        public UserStatus GetUserValidity(UserDetails u)
        {
            if(u.userlevel==0)
            {
                return UserStatus.AuthenticatedAdmin;
            }else if(u.userlevel == 1)
            {
                return UserStatus.AuthenticatedUser;
            }else
            {
                return UserStatus.NonAuthenticatedUser;
            }
        }

    }
}
